/*
 * @Author: c
 * @Date: 2023-12-03 02:18:10
 * @LastEditTime: 2024-02-27 16:02:29
 * @LastEditors: c
 * @Description: 
 * @FilePath: \music-web-backend\src\core\system\user\entities\user.entity.ts
 * https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// use/entities/user.entity.ts
import { Exclude } from 'class-transformer';
import { BcryptService } from 'src/common/service/bcrypt.service';
import { Artist } from 'src/core/music/artist/entities/artist.entity';
import { Song } from 'src/core/music/song/entities/song.entity';
import { Video } from 'src/core/music/video/entities/video.entity';
import { Role } from 'src/core/system/roles/entities/role.entity';
import { BeforeInsert, Column, Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn } from 'typeorm';

@Entity('user')
export class User {
  @PrimaryGeneratedColumn()
  id: number; //自增长id

  @Column({
    type: 'varchar',
    length: 64,
    nullable: true,
    default: null,
  })
  username: string; // 用户名

  @Column({
    type: 'varchar',
    length: 64,
    nullable: true,
    default: null
  })
  nickname: string;  //昵称


  @Column({
    type: 'tinyint',
    nullable: true,
    default: 0,
    comment: "状态(2:女;1:男)"
  })
  gender: number;  //性别

  @Column({
    type: 'varchar',
    length: 100,
    nullable: true,
    default: '',
    comment: "密码"
  })
  @Exclude()
  password: string;  // 密码

  @Column({
    type: 'varchar',
    length: 255,
    nullable: true,
    default: 'https://c-ssl.dtstatic.com/uploads/blog/202207/09/20220709150824_97667.thumb.400_0.jpg',
    comment: "头像"
  })
  avatar: string;   //头像

  @Column({
    type: 'bigint',
    nullable: true,
    comment: "手机号码"
  })
  mobile: number;  //手机号码


  @Column({
    type: 'tinyint',
    nullable: true,
    default: 1,
    comment: "用户状态(1:正常;0:禁用；2-禁言)"
  })
  status: number; //用户状态


  @Column({
    type: 'varchar',
    length: 128,
    nullable: true,
    default: null,
    comment: "邮箱"
  })
  email: string; //邮箱



  @Column({
    type: 'tinyint',
    nullable: true,
    default: 0,
    comment: "是否删除(1:删除;0:未删除)"
  })
  deleted: number; //是否删除



  @Column({
    name: 'create_time',
    type: 'timestamp',
    nullable: true,
    default: () => 'CURRENT_TIMESTAMP',
  })
  createTime: Date; //用户创建时间

  @Column({
    name: 'update_time',
    type: 'timestamp',
    nullable: true,
    default: () => 'CURRENT_TIMESTAMP',
  })
  updateTime: Date; //用户信息更新时间

  @Column({
    name: 'blocked_start_time',
    type: 'timestamp',
    nullable: true,
  })
  blockedStartTime: Date; //禁言开始时间

  @Column({
    name: 'blocked_end_time',
    type: 'timestamp',
    nullable: true,
  })
  blockedEndTime: Date; //禁言结束时间

  /**
    * 使用装饰器@BeforeInsert来装饰encryptPwd方法，表示该方法在数据插入之前调用，这样就能保证插入数据库的密码都是加密后的。
    */
  @BeforeInsert()
  async encryptPwd() {
    this.password = await BcryptService.hash(this.password || '123456')
  }

  // @OneToMany(() => Log, (log) => log.user)
  // @JoinColumn()
  // logs: Log;

  // @Column('simple-enum', { enum: ['root', 'author', 'visitor'] })
  // roles: string;   // 用户角色

  @ManyToMany(() => Role, (role) => role.users)
  @JoinTable({
    name: 'user_role', //生成的表名称
    joinColumn: {
      name: 'user_id',  //生成的列名称
      referencedColumnName: 'id' //关联的原始列名称
    },
    inverseJoinColumn: {
      name: "role_id",
      referencedColumnName: "id"
    }
  })
  roles: Role[];   // 用户角色

  //此用户喜欢的歌曲列表
  @ManyToMany(() => Song, song => song.favoriteUsers)
  favorSongList: Song[];   // 歌曲列表
  
  //此用户喜欢的歌手列表
  @ManyToMany(() => Artist, artist => artist.favoriteUsers)
  favorArtistList: Artist[];   // 歌手列表

  //此用户喜欢的视频列表
  @ManyToMany(() => Video, video => video.favoriteUsers)
  favorVideoList: Video[];   // 歌曲列表

  //此用户购买的歌曲列表
  @ManyToMany(() => Song, song => song.paidUsers)
  paidSongList: Song[];   // 歌曲列表


  constructor(partial: Partial<User>) {
    Object.assign(this, partial);
  }
}
